package com.freak.neteasecloudmusic.modules.find.recommend.base.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.freak.circularbeadimageview.CircularBeadClipPathImageView;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpFragment;
import com.freak.neteasecloudmusic.modules.find.recommend.base.fragment.adapter.RankingListAdapter;
import com.freak.neteasecloudmusic.modules.find.recommend.base.fragment.entity.RankingListEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class RankingListFragment extends BaseAbstractMvpFragment<RankingListPresenter> implements RankingListContract.View {
    @BindView(R.id.recyclerViewRankingList)
    RecyclerView recyclerViewRankingList;
    @BindView(R.id.circularBeadClipPathImageViewRankingListBg)
    CircularBeadClipPathImageView circularBeadClipPathImageViewRankingListBg;
    @BindView(R.id.textViewRankingListTitle)
    TextView textViewRankingListTitle;
    private RankingListAdapter rankingListAdapter;
    private List<RankingListEntity.PlaylistBean.TracksBean> list;
    private Bundle bundle;
    private String idx;

    @Override
    protected RankingListPresenter createPresenter() {
        return new RankingListPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_ranking_list;
    }

    @Override
    protected void initEventAndData() {

    }

    @Override
    protected void initView(View view) {
        bundle = getArguments();
        if (bundle == null) {
            return;
        }
        idx = bundle.getString("idx");
        switch (idx) {
            case "0":
                textViewRankingListTitle.setText("云音乐新歌榜");
                mPresenter.getRankingList(idx);
                break;
            case "1":
                textViewRankingListTitle.setText("云音乐热歌榜");
                mPresenter.getRankingList(idx);
                break;
            case "2":
                textViewRankingListTitle.setText("网易原创歌曲榜");
                mPresenter.getRankingList(idx);
                break;
            case "3":
                textViewRankingListTitle.setText("云音乐飙升榜");
                mPresenter.getRankingList(idx);
                break;
            case "4":
                textViewRankingListTitle.setText("云音乐电音榜");
                mPresenter.getRankingList(idx);
                break;
        }
        list = new ArrayList<>();
        rankingListAdapter = new RankingListAdapter(R.layout.view_item_ranking_list, list, mActivity);
        recyclerViewRankingList.setLayoutManager(new LinearLayoutManager(mActivity));
        rankingListAdapter.bindToRecyclerView(recyclerViewRankingList);
        recyclerViewRankingList.setAdapter(rankingListAdapter);
        recyclerViewRankingList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildLayoutPosition(view) != list.size()) {
                    outRect.top = 30;
                }

            }
        });


    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    protected void initLazyData() {

    }

    @Override
    protected void showLoading() {

    }

    @Override
    public void showToast(String toast) {

    }

    @OnClick({R.id.textViewRankingListTitle})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textViewRankingListTitle:

                break;
        }
    }

    @Override
    public void getRankingListSuccess(RankingListEntity model) {
        list.clear();
        if (model != null) {
            if (model.getPlaylist() != null) {
                if (model.getPlaylist().getTracks() != null && model.getPlaylist().getTracks().size() > 0) {
                    if (model.getPlaylist().getTracks().size() >= 3) {
                        for (int i = 0; i < 3; i++) {
                            list.add(model.getPlaylist().getTracks().get(i));
                        }
                    } else {
                        list.addAll(model.getPlaylist().getTracks());
                    }
                    Glide.with(mActivity).load(model.getPlaylist().getTracks().get(0).getAl().getPicUrl()).centerCrop().into(circularBeadClipPathImageViewRankingListBg);
                }
            }
        }
        rankingListAdapter.notifyDataSetChanged();
    }
}
